package com.ylbh.common.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ylbh.common.pojo.shortmessage.vo.SmsSelectTemplateVO;
import com.ylbh.common.pojo.shortmessage.vo.SmsTemplateVO;
import io.swagger.annotations.Api;

/**
 * @author: wjc
 * @date: 2019/10/14 11:07
 */
@Api("阿里云短信批量发送")
public class ExtensionBatchSms {

    public static SmsTemplateVO  ExtensionSendSms(String telephone, String templateCode){

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",SmsUtils.accessKeyId,SmsUtils.accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(SmsUtils.domain);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId","cn-hangzhou");
        request.putQueryParameter("PhoneNumberJson",telephone);
        request.putQueryParameter("SignNameJson",SmsUtils.signName);
        request.putQueryParameter("TemplateCode",templateCode);
        /*request.putQueryParameter("TemplateParamJson",""); */
        //request.putQueryParameter("TemplateParamJson",null);
        //request.putQueryParameter("TemplateParamJson","{\"？\":\"？\"}");
        CommonResponse response = new CommonResponse();
        try {
            response = client.getCommonResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println(response.getData());
        return JSON.parseObject(response.getData(), SmsTemplateVO .class);
    }
    //测试
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", SmsUtils.accessKeyId, SmsUtils.accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "13413325778,18922418334");
        request.putQueryParameter("SignName", SmsUtils.signName);
        request.putQueryParameter("TemplateCode", "SMS_177250842");
        //request.putQueryParameter("TemplateParamJson", null);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
